import { createStore } from 'vuex'
import { login, getUserinfo, getIndexMenu } from '~/api/manager'
import { setToken, setItem, getItem } from '~/utils/localStorage'
import { showMessage } from '~/utils/my-notification'
import { addLocalRouter, mapMenusToPermissions, removeDuplicates } from '~/utils/common'

const store = createStore({
  state() {
    return {
      // 用户信息
      user: {},
      // tag列表
      tagList: [{
        lable: '控制台',
        name: 'control',
        path: '/operation/control'
      }]
    }
  },
  mutations: {
    SET_USERINFO(state, user) {
      state.user = user
    },
    // 修改tag
    UPDATE_TAGS(state, tag) {
      state.tagList.push(tag)
      state.tagList = removeDuplicates(state.tagList)
    },
    // 关闭tag
    CLOSE_TAGS(state, tag) {
      const index = state.tagList.findIndex(val => val.name === tag.name)
      state.tagList.splice(index, 1)
    }
  },
  actions: {
    // 登录
    login({ commit }, { username, password }) {
      return new Promise((resolve, reject) => {
        login(username, password).then(res => {
          // 将token信息放入本地缓存中
          const data = res.data
          if(res && data.code === 200) {
            setToken(data.data.token)
            resolve(res)
          } else if(data.code === 500) {
            showMessage(data.message, 'error')
          }
        }).catch(err => reject(err))
      })
    },
    // 获取用户信息
    getUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        const userInfo = getItem('userinfo')
        if(userInfo) {
          commit('SET_USERINFO', userInfo)
          resolve(userInfo)
        } else {
          getUserinfo().then(res => {
            commit('SET_USERINFO', res.data)
            setItem('userinfo', res.data)
            resolve(res)
          }).catch(err => reject(err))
        }
      })
    },
    // 获取菜单栏
    getMenu({ commit }) {
      return new Promise((resolve, reject) => {
        const menu = getItem('menu')
        if(menu && menu.length > 0) {
          addLocalRouter(menu)
          const permissions = mapMenusToPermissions(menu)
          setItem('permissions', permissions)
        } else {
          getIndexMenu().then(res => {
            if(res && res.data.code === 200) {
              const data = res.data.data[0].children
              setItem('menu', data)
  
              addLocalRouter(data)
              const permissions = mapMenusToPermissions(data)
              setItem('permissions', permissions)
            }
            resolve(res)
          }).catch(err => reject(err))
        }

      })
    },
    // 获取菜单栏 刷新页面保持路由注册
    loadLocalAction() {
      const menu = getItem('menu')
      // console.log('menu', menu)
      if(menu && menu.length > 0) {
        addLocalRouter(menu)
      } else {
        getIndexMenu().then(res => {
          if(res && res.data.code === 200) {
            const data = res.data.data[0].children
            setItem('menu', data)

            addLocalRouter(data)
          }
        }).catch(err => {})
      }
    }
  }
})

export default store
